package demo4.mapper;

import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper {
    @Autowired
    private IDBUtil dbUtil;

    @Override
    public int insert(User user) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "INSERT INTO user(username,password,nickname,age)" +
                    "VALUES (?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public User selectByUsername(String username) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT username,password,nickname,age FROM user WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public void userList() {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT username,password,nickname,age FROM user";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getString("username") + "\t" + rs.getString("password") + " " + rs.getString("nickname") + " " + rs.getInt("age"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public List<User> selectAll(){
        List<User> list = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "SELECT id,username,password,nickname,age FROM user";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                list.add(user);
            }
           // return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;

    }
    public void userUpdate(User user){
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "UPDATE user SET password = ?,nickname = ?,age = ? WHERE username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getPassword());
            ps.setString(2, user.getNickname());
            ps.setInt(3, user.getAge());
            ps.setString(4, user.getUsername());
            int result = ps.executeUpdate();
            System.out.println(result>0?"更新成功":"更新失败,"+user.getUsername()+"不存在");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
